﻿using Uniforax.Core.Common;
using Uniforax.Core.GameActions;
using Uniforax.Core.GameObjects;

namespace Uniforax.Camp.TestData.BigWall
{
    public class UpgradeBigWallArmorAction : BaseGameAction
    {
        public PosId PosId { get; set; }
        public string ObjectId { get; set; }

        public BigWallArmorEnum OldBigWallArmor { get; set; }
        public UpgradeBigWallArmorAction(BaseGameObject gameObject)
        {
            PosId = gameObject.PosId;
            ObjectId = gameObject.ObjectId;
        }

        public override void Execute(ActionContext context)
        {
            var world = context.GameWorld;
            var grid = world.GetGrid(PosId);
            var gameObject = grid.GetPlacedObject(ObjectId);
            if (gameObject.ArmorManager is BigWallArmorManager bigWallArmorManager)
            {
                if (bigWallArmorManager.ArmorType == BigWallArmorEnum.Armor2)
                {
                    IsCancenlled = true;
                    return;
                }
                OldBigWallArmor = bigWallArmorManager.ArmorType;
                bigWallArmorManager.ArmorType = BigWallArmorEnum.Armor2;
            }
            else
            {
                IsCancenlled = true;
            }
        }

        public override void Undo(ActionContext context)
        {
            if (IsCancenlled == true) return;

            var world = context.GameWorld;
            var grid = world.GetGrid(PosId);
            var gameObject = grid.GetPlacedObject(ObjectId);
            if (gameObject.ArmorManager is BigWallArmorManager bigWallArmorManager)
            {
                bigWallArmorManager.ArmorType = OldBigWallArmor;
            }
            else
            {
                // DO NOTHING
            }
        }
    }
}
